/**
 * Copyright 2000 self.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 * 	The greatest achievement is selflessness. The greatest worth is self-mastery.
 * 	The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 * 	The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 * 	The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 * 	The greatest goodness is a peaceful mind. The greatest patience is humility.
 * 	The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 * 	The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 *
 * @author Ashin Wimalajeewa
 */
package self.amigo.editor.sheet;

import java.awt.BorderLayout;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import self.amigo.editor.APropertySheet;
import self.amigo.editor.cmd.PropertiesChange;
import self.amigo.elem.ButtonView;
import self.amigo.elem.InputBoxView;
import self.awt.layout.VerticalFlowLayout;
import self.gee.IEditableElement;
import self.gee.editor.IPropertySheet;

public class InputBoxSheet extends APropertySheet implements IPropertySheet {
	
	/**
	 * If the internal state of this class ever changes in such a way that it can't be defaulted,
	 * then the {@link #serialVersionUID} should be incremented to ensure serialized instances cleanly fail.  
	 */
	private static final long serialVersionUID = 1;
	
  JTabbedPane         propertySheet = new JTabbedPane();
  BorderLayout        contentPaneLayout = new BorderLayout();
  JPanel              generalTab = new JPanel();
  VerticalFlowLayout  generalPaneLayout = new VerticalFlowLayout();
  JPanel              generalPane = new JPanel();
  JScrollPane         generalScroller = new JScrollPane();
  BorderLayout        generalLayout = new BorderLayout();

  JLabel              textLabel = new JLabel();
  JTextArea           text = new JTextArea();
  JScrollPane         textScroller = new JScrollPane();

  ButtonGroup         btnGroup = new ButtonGroup();
  JRadioButton        singleLineEditType = new JRadioButton();
  JRadioButton        comboType = new JRadioButton();
  JRadioButton        memoType = new JRadioButton();

  JCheckBox           horizBar = new JCheckBox();
  JCheckBox           vertBar = new JCheckBox();
  
  JLabel aidLinkLabel = new JLabel();
  JTextField aidLink = new JTextField();
  
  private IEditableElement view;

  public InputBoxSheet() {
    try {
      jbInit();
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
  
  private void jbInit() throws Exception {
    this.setLayout(contentPaneLayout);

    aidLinkLabel.setDisplayedMnemonic('0');
    aidLinkLabel.setText("aid");    
    
    textLabel.setText("edit text");
    textLabel.setDisplayedMnemonic('T');
    textLabel.setLabelFor(text);

    text.setRows(10);
    text.setWrapStyleWord(true);

    btnGroup.add( singleLineEditType );
    btnGroup.add( comboType );
    btnGroup.add( memoType );
    
    singleLineEditType.setSelected(true);
    singleLineEditType.setText("single line edit");
//    singleLineEditType.setHorizontalTextPosition(SwingConstants.LEADING);
    
    comboType.setText("combo box");
//    comboType.setHorizontalTextPosition(SwingConstants.LEADING);

    memoType.setText("memo edit");
//    memoType.setHorizontalTextPosition(SwingConstants.LEADING);

    horizBar.setText("show horizontal scrollbar");
//    horizBar.setHorizontalTextPosition(SwingConstants.LEADING);
        
    vertBar.setText("show verical scrollbar");
//    vertBar.setHorizontalTextPosition(SwingConstants.LEADING);

    textScroller.getViewport().add(text, null);
    
    generalPane.setLayout(generalPaneLayout);
    generalPane.add(textLabel, null);
    generalPane.add(textScroller, null);

    generalPane.add(singleLineEditType, null);
    generalPane.add(comboType, null);
    generalPane.add(memoType, null);

    generalPane.add(horizBar, null);
    generalPane.add(vertBar, null);

    generalPane.add(aidLinkLabel, null);
    generalPane.add(aidLink, null);
    
    generalScroller.getViewport().add( generalPane, null );
    generalTab.setLayout(generalLayout);
    generalTab.add( generalScroller, BorderLayout.CENTER );
    
    this.add(propertySheet, BorderLayout.CENTER);
    propertySheet.add(generalTab, "General");
  }
  
  public void inspect( Object toDisplay ) {
    if ( toDisplay instanceof InputBoxView ) {
      view = (IEditableElement) toDisplay;
      revert();
    } else 
      throw new RuntimeException("Invalid Graphic element to display:" + toDisplay );
  }
  
  public void accept() {
    if ( view == null )
      return;
      
    store.put( InputBoxView.TEXT_PROP, text.getText() );
    store.put( InputBoxView.TYPE_PROP, new Integer(getType()) );
    store.put( InputBoxView.SCROLLBAR_PROP, new Integer(getScrollBars()) );
    store.put( InputBoxView.AID_PROP,  aidLink.getText() );
    PropertiesChange cmd = new PropertiesChange( editor, view, store );
    cmd.executeIfRequired();
  }
  
  public void revert() {
    if ( view == null )
      return;
      
    store.clear();
    view.getProperties( store );  
    text.setText( (String) store.get(InputBoxView.TEXT_PROP) );
    setType( ((Integer)store.get(InputBoxView.TYPE_PROP)).intValue() );
    setScrollBars( ((Integer)store.get(InputBoxView.SCROLLBAR_PROP)).intValue() );
    aidLink.setText( (String) store.get( ButtonView.AID_PROP ) );   
  }
  
  public Object getObjectUnderInspection() {
    return view;
  }
  
  private int getType() {
    if ( singleLineEditType.isSelected() )
      return InputBoxView.SINGLE_LINE_EDIT;
    if ( comboType.isSelected() )
      return InputBoxView.COMBO_BOX;
    return InputBoxView.MEMO_EDIT;
  }
  
  private int getScrollBars() {
    boolean h = horizBar.isSelected();
    boolean v = vertBar.isSelected();
    
    if ( h && v ) 
      return InputBoxView.BOTH_SCROLLBARS;
    if ( h ) 
      return InputBoxView.HORIZ_SCROLLBAR;
    if ( v ) 
      return InputBoxView.VERT_SCROLLBAR;    
    return InputBoxView.NO_SCROLLBARS;
  }
  
  private void setScrollBars( int val ) {
    if ( val == InputBoxView.NO_SCROLLBARS ) {
      horizBar.setSelected( false );
      vertBar.setSelected( false );
      return;
    }
    horizBar.setSelected( (val == InputBoxView.HORIZ_SCROLLBAR) || (val == InputBoxView.BOTH_SCROLLBARS) );
    vertBar.setSelected( (val == InputBoxView.VERT_SCROLLBAR) || (val == InputBoxView.BOTH_SCROLLBARS) );
  }
  
  private void setType( int val ) {
    switch ( val ) {
      case InputBoxView.MEMO_EDIT: memoType.setSelected( true ); break;
      case InputBoxView.COMBO_BOX: comboType.setSelected( true ); break;
      default: singleLineEditType.setSelected( true ); break;
    }
  }
  
//  static public void main( String[] args ) {
//    JFrame form = new JFrame( "test" );
//    InputBoxSheet sheet = new InputBoxSheet();
//    form.getContentPane().add( sheet );
//    form.show();
//  }
}
